<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// 每隔十分钟执行
class Token extends CI_Controller {

	public function index(){
		$tokenfile = BASEWEBPATH . '/other/accesstoken.txt';
		$re = @file_get_contents($tokenfile);
		if ($re === false){
			$this->load->model('Wxident_model', 'Mwxident');
			$this->Mwxident->get_access_token_by_api();
			exit;
		}
		$re_arr = json_decode($re, true);
		if (isset($re_arr['expires_in']) && isset($re_arr['get_time'])){
			$existed = time() - $re_arr['get_time'];
			if ($existed >= ($re_arr['expires_in'] - 30 * 60)){
				// 失效前30分钟重新获取
				$this->load->model('Wxident_model', 'Mwxident');
				$this->Mwxident->get_access_token_by_api();
			}
		}
	}
	
}
